package practise.alogrithm.hundredInterviewQuestions.secondTime;

import java.util.Stack;

/**
 * Created by Richard on 7/25/2014.
 */
public class StackContainMin {

    Stack<Integer>  stack = new Stack<>();
    Stack<Integer> stackForMin = new Stack<>();

    public void push (Integer object){

        if(stackForMin.isEmpty()){
            stackForMin.push(object);
        }
        else if(object<stackForMin.peek()){

            stackForMin.push(object);
        }
        else
            stackForMin.push(stackForMin.peek());

        stack.push(object);
    }

    public Integer pop(){
        stackForMin.pop();
        return stack.pop();
    }

    public Integer min(){
        return stackForMin.peek();
    }

    public boolean isEmpty(){
        return stack.isEmpty();
    }

    public static void main(String[] args){
        StackContainMin stackContainMin = new StackContainMin();
        stackContainMin.push(3);
        stackContainMin.push(4);
        stackContainMin.push(5);
        stackContainMin.push(2);
        stackContainMin.push(1);
        while (!stackContainMin.isEmpty()){
            System.out.println("min:"+stackContainMin.min());
            System.out.println("out:"+stackContainMin.pop());

        }
    }
}
